require "rubygems"
require "action_mailer"

define_extension :deployment_notification, :mail do
#  def notify_deployment_success
#  end
#  
#  def notify_deployment_failure(error)
#  end
#  
#  def notify_deployment_success_summary
#  end
#  
#  def notify_deployment_failure_summary(error)
#  end
end

define_extension :task_notification, :mail do
  module ::Wondercap
    module MailNotifiers
      class Mailer < ActionMailer::Base
        def notification(from, to, subject, body)
          self.from from
          self.recipients to
          self.subject subject
          self.body body
        end
      end      
    end
  end
  
  set :_default_per_configuration_subject, %q{<%= task_name.capitalize %> <%= status.to_s.upcase %> (<%= area.name %>): <%= project_name %>}
  
  set :_default_per_configuration_body, <<-'EOF'
Name: <%= project_name %>
Task: <%= task_name %>
Area: <%= area.name %>
Revision in area: <%= installed_revision_description %>
Status: <%= status %>

% if exists?(:error)
Error: <%= error %>
<%= error.backtrace.join("\n\t") %>
% end
Log:
<%= logger_output %>      
EOF
  
  set :_default_summary_subject, %q{<%= task_name.capitalize %> <%= status.to_s.upcase %> (<%= area.name %>): <%= outer_configuration.names.sort.join %>}
  
  set :_default_summary_body, <<-'EOF'
Task: <%= task_name %>
Status: <%= status.to_s.upcase %>
Destination area: <%= area.name %>
Supplied names (after filtering): <%= outer_configuration.names %>

% if exists?(:error) && error.respond_to?(:errors)
Names ([S] means succeeded):
<% outer_configuration.names.each do |name| %>
 * <%= name %><%= error.errors.find { |conf, e| conf.project_name == name } ? "" : " [S]"%>
% end
% elsif exists?(:error)
Error: <%= error %>
<%= error.backtrace.join("\n\t") %>

% else
Successful names:
% outer_configurations.names.each do |name|
 * <%= name %>
%end
%end

% case fetch(:mail_notification_log_mode, :errors_only)
% when :total
Log:
<%= outer_configuration.logger_output %>      
% when :errors_only
% if error && error.respond_to?(:errors)
<% outer_configuration.names.each do |name| %>
% conf_error = error.errors.find { |conf, e| conf.project_name == name }[1]; next unless conf_error
* <%= name %>
Error: <%= conf_error %>
<%= conf_error.backtrace.join("\n\t") %>

Log:
<%= outer_configuration.mc.find_configuration(name).logger_output %>
--------------------------------------------------------------------------------

% end
% end
% end
EOF
    
  task(:_notify) do
    subject = ERB.new(fetch(:mail_notification_subject, _default_subject), nil, "%<>").result(binding)
    body = ERB.new(fetch(:mail_notification_body, _default_body), nil, "%<>").result(binding)
    Wondercap::MailNotifiers::Mailer.deliver_notification(fetch(:mail_notification_from, mail_notification_to), mail_notification_to, subject, body)
  end
  
  set(:installed_revision_description) do
    installed_revision_deployment ? installed_revision_deployment.real_revision : "not found"
  end  
  
  task :notify_task_success do
    execute_previous
    
    set :status, :success
    set :_default_subject, _default_per_configuration_subject
    set :_default_body, _default_per_configuration_body
    _notify
  end
  
  task :notify_task_failure do
    execute_previous
    
    set :status, :failure
    set :_default_subject, _default_per_configuration_subject
    set :_default_body, _default_per_configuration_body
    _notify    
  end
  
  task :notify_task_success_summary do
    execute_previous
    
    set :status, :success
    set :_default_subject, _default_summary_subject
    set :_default_body, _default_summary_body
    _notify    
  end
  
  task :notify_task_failure_summary do
    execute_previous
    
    set :status, :failure
    set :_default_subject, _default_summary_subject
    set :_default_body, _default_summary_body
    _notify    
  end
  
end

